<?php
/*
 * Aclass_list 数据类
 * 2011-8-29
 * http://www.abans.cn		abans@qq.com
 */
class Aclass_list extends Acan{
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}
	function cache_name($table){
		return 'Acan_list:'.$table.Afun_uid();
	}
	function delcache($table){
		return Afun_cache_rm($this->cache_name($table));
	}
	function form_json($model){
		//$datajson = Afun_cache_get($this->cache_name($model->table));
		//if(empty($datajson)){
			$data->key = $model->key_name;
			$data->form = $model->form;
			$data->enum = $model->enum;
			$data->check = $model->check;
			$data->comment = $model->comment;
			$data->table = $model->table;
			if(is_object($model->regexp) || is_array($model->regexp)){
				foreach ($model->regexp as $k => $v) {
					$regexp .="\"$k\":".$v.",";
				}
				$regexp = ",regexp:{".substr($regexp,0,-1)."}";
			}
			$datajson = substr(Ajson_encode($data),0,-1).$regexp.'}';
		//	Afun_cache_set($this->cache_name($model->table),$datajson,3600);
		//}
		return $datajson;
	}
	//--处理列数据显示
	function for_html($key,$val,$column,$model,$objname=''){
		if(is_object($val)){
			$val = Afun_obj_arr($val);
		}
		foreach ($column as $k => $v) {
			$obj->key = $model->key_name;
			$obj->fields = $k;
			$obj->id = $val[$obj->key];
			$obj->value = $val[$k];
			$json = Ajson_encode($obj);
			if($k == $model->key_name){
				$html .="<td>";
				$html .= '<input type="checkbox" class="checkbox" name="chk[]" value="'.$val[$k].'">'.$val[$k];
			}else{
				if(!empty($objname)){$click = "onclick='$objname.edit(this)'";}else{$click ='';}
				$html .="<td id='data-$obj->id-$obj->fields' $click>";
				if(!empty($model->enum[$k])){
					$style = 'class="dis_none"';
					if(is_object($model->enum) || is_array($model->enum)){
						$enum = $model->enum;
					}else{
						$enum = Afunc_enum()->data($model->enum[$k]);
					}
					if($model->form[$k]->type=='checkbox'){//checkbox合并显示枚举信息
						$strr = json_decode($val[$k]);
						if(!is_array($strr)){continue;}//无数据跳过
						foreach($strr as $m=>$n){
							if(empty($strrs)){
								$strrs = $enum[$k][$n];
							}else{
								$strrs .= ','.$enum[$k][$n];
							}
						}
						$enum_val = '<a>'.$strrs.'</a>';
					}else{
						$enum_val = '<a>'.$enum[$k][$val[$k]].'</a>';
					}
				}elseif($model->form[$k]->type=='image'){
					$enum_val = '<a><img src="'.$val[$k].'"></a>';
					$style = 'class="dis_none"';
				}else{
					$style = '';
					$enum_val = '';
				}
				$html .= $enum_val."<span $style>";
				if($model->columns['type'][$k]=='datetime'){
					$html .= $val[$k];
				}else{
					$html .= $val[$k];
				}
				$html .= '</span>';
			}
			$html .="</td>";
		}
		return $html;
	}
	//--栏目数据设置{输入 - $model:数据模型;$arr:指定的默认显示栏目;$post:POST提交数据; 输出 - 合并POST更改后的栏目数据}
	function column($model,$arr,$post){
		$cache_name = 'Acan-column:'.$this->Aactr.':'.$model->table;
		$data = Afun_cache_get($cache_name);
		if(!is_array($data)){$data = $arr;}
		if($post){
			$data = $post;
			Afun_cache_set($cache_name,$data,3600);//写入缓存
		}
		return $data;
	}
	//--栏目设置html{输入 - $model:数据模型;$column:栏目数据 输出 - 栏目设置的html代码}
	function column_html($model,$column,$objname){
		//--补充自定义栏目
		foreach ($column as $key => $val) {
			if($model->comment[$key] && empty($val)){
				$column[$key] = $model->comment[$key];
			}elseif(empty($model->comment[$key])){
				$model->comment[$key] = $val;
			}
		}
		foreach ($model->comment as $key => $val) {
			$check = '';
			if(in_array($key,array_keys($column))){
				$check = 'checked="checked"';
				$color = '-green';
				$mark = '√';
			}else{
				$color = '-brown';
				$mark = '×';
			}
			if($model->comment[$key]){
				$vkey = $model->comment[$key];
			}else{$vkey = $key;}
			$column_html .= '<p onclick="'.$objname.'.column_chk(\''.$key.'\')" class="column-'.$key.'">';
			$column_html .= '<span class="_mark '.$color.'">'.$mark.'</span>'.$vkey;
			$column_html .= '<input class="dis_none" type="checkbox" name="acolumn['.$key.']" value="'.$vkey.'" '.$check.' />';
			$column_html .= '</p>';
		}
		return $column_html;
	}

}

?>
